/*
 * @User 		@Company 	@Date
 * marcusbui	VNNET24  	Jun 19, 2011
 */
package tsb.com.html5.controller.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import tsb.com.html5.common.util.AppUtil;
import tsb.com.html5.controller.base.BaseController;
import tsb.com.html5.product.item.ProductItem;
import tsb.com.html5.product.item.ProductSubItem;
import tsb.com.html5.product.param.GetProductParam;
import tsb.com.html5.product.service.IProductService;

// TODO: Auto-generated Javadoc
/**
 * The Class ProductController.
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController{

	/** The product service. */
	@Resource(name="ProductService")
	private IProductService productService;
	
	/**
	 * Gets the product by category.
	 *
	 * @param id the id
	 * @param limit the limit
	 * @return the product by category
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/get-product-by-cate", method = RequestMethod.GET)
	protected @ResponseBody Map<String, ? extends Object> getProductByCategory(
			@RequestParam String id, @RequestParam(required=false) Object limit) throws Exception {
		HashMap<String, Object> returnMap = new HashMap<String, Object>(); 
		try {
			if(!AppUtil.isNullOrEmpty(id)){
				GetProductParam param = new GetProductParam();
				param.setCateId(id);
				
				if(limit!=null && AppUtil.tryParseInt(limit)){
					int limitNo = Integer.parseInt((String)limit);
					param.setLimitNumberOfProduct(limitNo);
				}
				
				List<ProductItem> productList = productService.getProduct(param);
				if(!AppUtil.isListEmpty(productList)){
					returnMap.put("productList", productList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * Gets the sub product.
	 *
	 * @param id the id
	 * @return the sub product
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/sub-product-detail", method = RequestMethod.GET)
	protected @ResponseBody Map<String, ? extends Object> getSubProduct(@RequestParam String id) throws Exception {
		HashMap<String, Object> returnMap = new HashMap<String, Object>(); 
		try {
			if(!AppUtil.isNullOrEmpty(id)){
				GetProductParam param = new GetProductParam();
				param.setProductId(id);
				
				List<ProductSubItem> productSubList = productService.getProductSub(param);
				if(!AppUtil.isListEmpty(productSubList)){
					returnMap.put("productSubList", productSubList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * Gets the product by id.
	 *
	 * @param id the id
	 * @return the product by id
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/product-detail", method = RequestMethod.GET)
	protected @ResponseBody Map<String, ? extends Object> getProductById(@RequestParam String id) throws Exception {
		HashMap<String, Object> returnMap = new HashMap<String, Object>(); 
		try {
			if(!AppUtil.isNullOrEmpty(id)){
				GetProductParam param = new GetProductParam();
				param.setProductId(id);
				
				List<ProductItem> productList = new ArrayList<ProductItem>();
				List<ProductItem> cacheProductList = cacheController.getProduct();
				for(ProductItem item : cacheProductList){
					if(id.equals(item.getSeq())){
						productList.add(item);
						break;
					}
				}
				
//				List<ProductItem> productList = productService.getProduct(param);
				
				if(!AppUtil.isListEmpty(productList)){
					returnMap.put("productList", productList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
	
}
